package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.BmiMapper;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.Bmi;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.state.UserSexEnum;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.UserSexVO;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author GaoYiJie
 * @Description
 * @Create 2025/3/6 9:20
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    BmiMapper bmiMapper;

    @Override
    public R sendPhoneCode(String phone) {

        //判断是否是平台用户
        String msg = "";
        User userDB = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getPhone, phone).last("limit 1"));
        if (userDB == null) {
            //进行注册
            User user = new User();
            user.setPhone(phone);
            userMapper.insert(user);
            msg = "注册成功";
        }

        //发送验证码
        String phoneCode = RandomUtil.randomNumbers(4);
        System.out.println("验证码:" + phoneCode);
        MobileMessageUtil.sendMobileMessage(phone, phoneCode);

        String phoneKey = "phone_code:" + phone;
        stringRedisTemplate.opsForValue().set(phoneKey, phoneCode, 5, TimeUnit.MINUTES);

        return R.OK(msg);
    }

    @Override
    public R uploadImage(MultipartFile file) {

        try {
            String exName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), exName, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            System.out.println(url);
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }

    }

    @Override
    public R getSexList() {
        ArrayList<UserSexVO> userSexVOS = new ArrayList<>();
        UserSexEnum[] values = UserSexEnum.values();
        for (UserSexEnum sexEnum : values) {
            UserSexVO userSexVO = new UserSexVO();
            userSexVO.setName(sexEnum.getMsg());
            userSexVO.setValue(sexEnum.getCode());
            userSexVOS.add(userSexVO);
        }
        return R.OK(userSexVOS);
    }

    @Override
    public R getBmiList() {
        List<Bmi> bmis = bmiMapper.selectList(null);
        //计算BMI
        //体重的千克数除以身高平方(米为单位)
        for (Bmi bmi : bmis) {
            Double bmiValue = bmi.getWeight()/(bmi.getHigh()*0.01* bmi.getHigh()*0.01);
            bmi.setBmi(bmiValue);
        }

        return R.OK(bmis);
    }


}
